<html>
<head><meta charset="utf-8"><title>New assist for converting tuple variant to struct variant · t-compiler/rust-analyzer · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/index.html">t-compiler/rust-analyzer</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/New.20assist.20for.20converting.20tuple.20variant.20to.20struct.20variant.html">New assist for converting tuple variant to struct variant</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="241611750"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/New%20assist%20for%20converting%20tuple%20variant%20to%20struct%20variant/near/241611750" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Warner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/New.20assist.20for.20converting.20tuple.20variant.20to.20struct.20variant.html#241611750">(Jun 05 2021 at 02:45)</a>:</h4>
<p>Hi! Aspiring new contributor here. I noticed there's an existing convert_tuple_struct_to_named_struct assist, and I've been toying with copy/paste/munging that into an assist that will do the analogous thing for tuple variants - e.g. <code>enum E { V(usize) }</code> -&gt; <code>enum E { V { field1: usize } }</code></p>
<p>I think I've gotten the hang of code layout/etc, and I have a new <a href="http://convert_tuple_variant_to_named_variant.rs">convert_tuple_variant_to_named_variant.rs</a> file, with content _mostly_ stolen from the original file. I've adapted some tests (which now fail, of course), as well as some of the code. For instance, it was fairly straight forward to turn <code>edit_struct_def</code> into <code>edit_variant_def</code> that does the obvious thing - and _seems_ to work.</p>
<p>The trouble comes with <code>edit_struct_references</code>, which starts with:</p>
<div class="codehilite"><pre><span></span><code>    let strukt_def = Definition::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Struct(strukt)));
    let usages = strukt_def.usages(&amp;ctx.sema).include_self_refs().all();
</code></pre></div>
<p>However, it's not obvious to me how to do the analogous thing to find the usages of a variant. Any tips?<br>
Thanks!</p>



<a name="241615112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/New%20assist%20for%20converting%20tuple%20variant%20to%20struct%20variant/near/241615112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Warner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/New.20assist.20for.20converting.20tuple.20variant.20to.20struct.20variant.html#241615112">(Jun 05 2021 at 04:23)</a>:</h4>
<p>Aha! Nevermind, figured it out. <code>Definition::ModuleDef(hir::ModuleDef::Variant(variant))</code> was the ticket.</p>



<a name="241616096"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/185405-t-compiler/rust-analyzer/topic/New%20assist%20for%20converting%20tuple%20variant%20to%20struct%20variant/near/241616096" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Warner <a href="https://rust-lang.github.io/zulip_archive/stream/185405-t-compiler/rust-analyzer/topic/New.20assist.20for.20converting.20tuple.20variant.20to.20struct.20variant.html#241616096">(Jun 05 2021 at 04:51)</a>:</h4>
<p>Yay! <a href="https://github.com/rust-analyzer/rust-analyzer/pull/9144">https://github.com/rust-analyzer/rust-analyzer/pull/9144</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>